package models

import (
	"gorm.io/gorm"
	"manuscript-service/manrpc/manrpc"
)

type Gao struct {
	gorm.Model
	Name  string `gorm:"type:varchar(50);not null;comment:'姓名'"`
	Like  int    `gorm:"type:int;not null;comment:'点赞'"`
	Image string `gorm:"type:varchar(255);not null;comment:'图片'"`
	Fid   int    `gorm:"type:int;not null;comment:'类型id'"`
}

func (g *Gao) GaoAdd(db *gorm.DB) error {
	return db.Debug().Create(g).Error
}

func (g *Gao) GaoShow(db *gorm.DB, name string) error {
	return db.Debug().Where("name= ?", name).First(g).Error
}

type Fen struct {
	gorm.Model
	Title string `gorm:"type:varchar(30);not null;comment:'类型'"`
}

func (f *Fen) FenList(db *gorm.DB, title string) error {
	return db.Debug().Where("title", title).FirstOrCreate(f).Error
}

type GaoFenList struct {
	Gid   int
	Name  string
	Like  int
	Image string
	Title string
}

func (l *GaoFenList) GaoListss(db *gorm.DB, likes []*manrpc.GaoList, in *manrpc.GaoListRequest) ([]*manrpc.GaoList, error) {
	err2 := db.Debug().Model(&l).Table("gaos").
		Select(
			"gaos.id as gaoId",
			"gaos.name",
			"gaos.like",
			"gaos.image",
			"fens.title",
		).Joins("LEFT JOIN fens ON fens.id = gaos.fid").
		Find(&likes).Error
	return likes, err2
}
